/**
 * @description 生成唯一 uuid
 * @returns {String}
 */
export function generateUUID() {
  let uuid = "";
  for (let i = 0; i < 32; i++) {
    let random = (Math.random() * 16) | 0;
    if (i === 8 || i === 12 || i === 16 || i === 20) uuid += "-";
    uuid += (i === 12 ? 4 : i === 16 ? (random & 3) | 8 : random).toString(16);
  }
  return uuid;
}

/**
 * @description 将数字转换为字符串，按10000添加分隔符
 * @param {Number} num 数字
 * @returns {String}
 */
export function formatNumber(num: number) {
  return num.toString().replace(/\B(?=(\d{4})+(?!\d))/g, ",");
}

/**
 * @description 计算缩放后的坐标
 * @param {Number} originalWidth 原始图片宽度
 * @param {Number} originalHeight 原始图片高度
 * @param {Number} x 原始坐标x
 * @param {Number} y 原始坐标y
 * @param {Number} containerWidth 容器宽度(默认672)
 * @param {Number} containerHeight 容器高度(默认340)
 * @returns {Object} 缩放后的坐标
 */
export function calculateScaledCoordinates(
  originalWidth: number,
  originalHeight: number,
  x: number,
  y: number,
  containerWidth = 672,
  containerHeight = 340
) {
  // 计算宽高比例
  const widthRatio = containerWidth / originalWidth;
  const heightRatio = containerHeight / originalHeight;

  // 选择较大的比例以保持等比缩放
  const scaleRatio = Math.min(widthRatio, heightRatio);

  // 计算缩放后的图片尺寸
  const scaledWidth = originalWidth * scaleRatio;
  const scaledHeight = originalHeight * scaleRatio;

  // 计算缩放后图片的位置
  const offsetX = (containerWidth - scaledWidth) / 2;
  const offsetY = (containerHeight - scaledHeight) / 2;

  // 计算缩放后的坐标
  const scaledX = x * scaleRatio + offsetX;
  const scaledY = y * scaleRatio + offsetY;

  return {
    scaledX,
    scaledY,
  };
}
/**
 * @description 计算缩放后的图片位置和缩放比例
 * @param imgWith 
 * @param imgHeight 
 * @param containerWidth 
 * @param containerHeight 
 * @returns {Object} 缩放后的位置和缩放比例
 */
export function calculateScaledSizeAndPosition(
  imgWith: number,
  imgHeight: number,
  containerWidth = 672,
  containerHeight = 340
) {
  // 计算宽高比例
  const widthRatio = containerWidth / imgWith;
  const heightRatio = containerHeight / imgHeight;

  // 选择较大的比例以保持等比缩放
  const scaleRatio = Math.min(widthRatio, heightRatio);

  // 计算缩放后的图片尺寸
  const scaledWidth = imgWith * scaleRatio;
  const scaledHeight = imgHeight * scaleRatio;

  // 计算缩放后图片的位置
  const left = (containerWidth - scaledWidth) / 2;
  const top = (containerHeight - scaledHeight) / 2;

  return {
    left,
    top,
    scaleRatio,
  };
}

// 生成路径string
export const generatePathString = (
  imgWith: number,
  imgHeight: number,
  points: any[],
  containerWidth = 672,
  containerHeight = 340
) => {
  let pathString = "M ";
  const pointsString =
    points
      .map((point: any) => {
        const { scaledX, scaledY } = calculateScaledCoordinates(
          imgWith,
          imgHeight,
          point[0],
          point[1],
          containerWidth,
          containerHeight
        );
        return `${scaledX} ${scaledY} `;
      })
      .join("L ") + " z";

  pathString += pointsString;
  return pathString;
};

/**
 * @description 判断文本是否符合条件
 * @param {String} text 文本
 * @returns {Boolean} 是否符合条件
 */
export function isTextValid(text: string) {
  // 从文本中提取数字
  const numbers = text.match(/\d+/g);
  if (!numbers || numbers.length === 0) return false;
  const number = Number(numbers.join(""));
  // 判断数字是否大于等于10000, 小于500000
  if (number < 10000 || number >= 500000) return false;
  return true;
}

export function throttle<T extends any[]>(func: (...args: T) => void, delay: number) {
  let lastTime = 0;

  return function(this: unknown, ...args: T) {
      const now = Date.now();
      if (now - lastTime >= delay) {
          lastTime = now;
          func.apply(this, args);
      }
  };
}
